home *** CD-ROM | disk | FTP | other *** search
/ Gamers Delight 2 / Gamers Delight 2.iso / Aminet / game / misc / robots.lha / player / BlindSchleiche.r < prev    next >
Text File  |  1993-04-25  |  4KB  |  251 lines

  1. Sirius Cybernetic
  2. Felix Buebl & Michael Balser
  3. /* This robot does
  4.    a) move randomly
  5.    b) search vor the enemy (360 degree scan!)
  6.    c) try to watch() it where it is moving to (calculate it's heading)
  7.    d) fire at it
  8.  
  9. */
  10.  
  11. /* the robot's own data: */
  12. int degree, turn;
  13. /* enemies has the following meaning:
  14.      0:  Robot is searching for an enemie
  15.    1++:  Robot has found  # enemies
  16.    */
  17. int enemies;
  18. /* this "array" stores the coords where the current enemie has been last */
  19. int x1, y1, x2, y2;
  20.  
  21.  
  22. main()
  23. {
  24. /* initialize */
  25. enemies = turn = degree = x1 = y1  = y2 = 0;
  26. x2 = 1234;
  27.  
  28. while (666 != 42) /* for ever */
  29.       {
  30.        move();  /* is invoked EVERY loop! */
  31.  
  32.        if (!enemies)
  33.           search();
  34.        else
  35.           {
  36.            if (x2 == 1234)
  37.               watch();
  38.            else
  39.               terminate();
  40.           }
  41.       }
  42. }
  43.  
  44. move ()
  45. {
  46. /* IF we are close to a wall: Retreat 8c) */
  47.  
  48.  
  49. if (loc_x() > 700)
  50.    {
  51.     degree =  160 + rand(40);
  52.        drive (degree, 100);
  53.     return;
  54.    }
  55.  
  56. if (loc_x() < 300)
  57.    {
  58.     degree =  -20 + rand(40);
  59.     drive (degree, 100);
  60.     return;
  61.    }
  62.  
  63. if (loc_y() > 700)
  64.    {
  65.     degree =  250 + rand(40);
  66.     drive (degree, 100);
  67.     return;
  68.    }
  69.  
  70. if (loc_y() < 300)
  71.    {
  72.     degree =  70 + rand(40);
  73.     drive (degree, 100);
  74.     return;
  75.    }
  76.  
  77. /* if the ran into some enemie, retreat */
  78.  
  79. if (!speed())
  80.    {
  81.     degree -= 180;
  82.     drive (degree, 100);
  83.     return;
  84.    }
  85.  
  86. /* if we are not close to a wall, PERHAPS change movent */
  87.  
  88.  
  89.  
  90. /* if we want to turn, do it */
  91.  
  92. if (turn && speed() >= 50 )
  93.     drive (degree, 42);
  94.  
  95. if (turn && speed() < 50)
  96.    {
  97.    degree += turn;
  98.    turn = 0;
  99.    drive (degree, 100);
  100.    }
  101.  
  102. if (!turn && x2 != 1234)
  103.    {
  104.     drive (degree, 100);
  105.    }
  106.  
  107.  
  108. if (rand(21) == 1  && !turn  )
  109.    {
  110.    if (degree > 180)
  111.       turn = (-1)*rand(10)*6 + 30;
  112.    else
  113.       turn = rand (10)*6 -30;
  114.    }
  115. return;
  116. }
  117.  
  118.  
  119. search ()
  120. {
  121. int i, range, dir;
  122.  
  123. /* do a partially 360 degrees scan first in order to find a enemie */
  124. i=18;
  125.  
  126. while (i > 0)
  127.     {
  128.      if (range = scan ( i*20, 10) )
  129.      if (range < 850 )
  130.         {
  131.          dir =  i*20;
  132.          enemies = 1;
  133.          i=0; /*break*/
  134.         }
  135.      i--;
  136.     }
  137.  
  138. if (!enemies)
  139.     return;
  140.  
  141. /* now get the exact position */
  142.  
  143. i= -6;
  144. while ( i<= 6)
  145.   {
  146.    if ( range = scan ( dir + i*3, 1))
  147.       {
  148.        dir += i*3;
  149.        i = 42; /* break */
  150.       }
  151.    i++;
  152.   }
  153. /* evaluate the coords */
  154.  
  155. x1 = loc_x() + cos(dir)*range/100000;
  156. y1 = loc_y() + sin(dir)*range/100000;
  157. x2 = 1234;
  158. return;
  159.  
  160. }
  161.  
  162. watch()
  163. {
  164.  int scandir, newrange, i;
  165.  int xh,yh;
  166.  
  167.  xh=loc_x()-x1;
  168.  yh=loc_y()-y1;
  169.  
  170.  if (xh>0)
  171.      scandir = atan (yh*100000/xh) + 180; /* +-90 */
  172.  else
  173.      scandir = atan (yh*100000/xh);
  174.  
  175.  if (scandir < 0)
  176.     scandir += 360;
  177.  
  178.  i=0;
  179.  while ((! (newrange = scan (scandir+i*10, 5)) ) &&  i< 3 )
  180.        {
  181.         if (i <= 0)
  182.            i  = -i+1;
  183.         else
  184.            i *= -1;
  185.  
  186.        }
  187.  
  188.  if (!newrange)
  189.     {
  190.      enemies = 0;
  191.      return;
  192.     }
  193.  scandir += i*10;
  194.  
  195.  i=0;
  196.  while (( ! (newrange = scan (scandir + i*3, 1)) ) && i < 2)
  197.        {
  198.         if ( i <= 0)
  199.            i = -i +1;
  200.         else
  201.            i *= -1;
  202.        }
  203.  scandir += i*3;
  204. /*
  205.  if ( sqrt ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) > 123)
  206.     {
  207.     enemies = 0;
  208.     x2 = 1234;
  209.     }
  210. */
  211.     x2 = loc_x() + cos (scandir)*newrange/100000;
  212.     y2 = loc_y() + sin (scandir)*newrange/100000;
  213.  drive (degree, 0);
  214.  
  215.  return; 
  216. }
  217.  
  218. terminate()
  219. {
  220. int xt, yt, xh, yh, shootdir, shootrange;
  221.  
  222. xt = x2 + x2-x1;
  223. yt = y2 + y2-y1;
  224.  
  225.  if (xt-loc_x() < 0)
  226.      shootdir = atan ((yt-loc_y())*100000/(xt-loc_x())) + 180; /* +-90 */
  227.  else
  228.      shootdir = atan ((yt-loc_y())*100000/(xt-loc_x()));
  229.  
  230.  if (shootdir < 0)
  231.     shootdir += 360;
  232.  
  233.  xh=xt-loc_x();
  234.  yh=yt-loc_y();
  235.  if ( (shootrange = sqrt(xh*xh + yh*yh)) > 721)
  236.     {
  237.      y1 = y2;
  238.      x1 = x2;
  239.      return;
  240.     }
  241.  
  242.  
  243.  cannon (shootdir, shootrange);
  244.  y1 = y2;
  245.  x1 = x2;
  246.  x2 = 1234;
  247.  
  248.  return; 
  249. }
  250.  
  251.